﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ELMWCFServiceClass.ELMWCF;
using System.ComponentModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Modularity;
using System.Collections.ObjectModel;
using ELMApplication.Common.Const;
using System.Linq;
using ELMApplication.Common.UC;
using ELMApplication.Common.Util;

namespace SettingModule.Views.ELMItemsView
{
    public class ELMItemsPrentationModel : IELMItemsPrentationModel
    {

         private ELMServiceClient _domaincontext;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

        public ELMItemsPrentationModel(IELMItemsView view, ELMServiceClient domaincontext, IEventAggregator eventAggregator, IRegionManager regionManager, IModuleManager moduleManager)
        {
            this._domaincontext = domaincontext;            
            this._eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.moduleManager = moduleManager;
            this.View = view;           
            view.SetModel(this);
            this.ELMListData.DataLoadCompleted += new EventHandler(ELMListData_DataLoadCompleted);
            ELMListData.EditEvent += new EventHandler<ELMApplication.Common.Events.ELMListEventArgs>(hmList_EditEvent);
            ELMListData.DelEvent += new EventHandler<ELMApplication.Common.Events.ELMListEventArgs>(hmList_DelEvent);
            ELMListData.NewEvent += new EventHandler<ELMApplication.Common.Events.ELMListEventArgs>(ELMListData_NewEvent);
            //initialData();
        }

        void ELMListData_NewEvent(object sender, ELMApplication.Common.Events.ELMListEventArgs e)
        {
            CurrentElmItem = new ELMItem();
            CurrentElmItem.ELMItemId = Guid.NewGuid();
            CurrentElmItem.ELMList = CurrentElmList;
            ShowDetial(PrivegeTypes.New, "");
        }
        void hmList_DelEvent(object sender, ELMApplication.Common.Events.ELMListEventArgs e)
        {
            MessageBoxResult res = MessageBox.Show("确定删除？", "", MessageBoxButton.OKCancel);
            if (res == MessageBoxResult.OK)
            {
                delItem(e.DataKey);
            }
        }

        void hmList_EditEvent(object sender, ELMApplication.Common.Events.ELMListEventArgs e)
        {
            // model.SetAddingHuman();
            ShowDetial(PrivegeTypes.Edit, e.DataKey);
        }

        void ELMListData_DataLoadCompleted(object sender, EventArgs e)
        {
            BusyMessage.Close();
        }

        public void initialData()
        {
            BusyMessage.Show("数据读取中...");
            _domaincontext = new ELMServiceClient();         

            _domaincontext.GetELMItemsByELMListIdAsync(CurrentElmList.ELMListId);
            _domaincontext.GetELMItemsByELMListIdCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        ELMListData.LoadCommonData(CurrentElmList.ContentType.ContentId, data.Result);
                    }
                    else throw data.Error;
                };      
        }
        

       

        #region IAddUserPrentationModel Members

        public IELMItemsView View
        {
            get;
            set;
        }

       
      

        #endregion

        #region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

      

  
        #region IAddUserPrentationModel Members

        private ContentType _extendInfo;
        public ContentType ExtendInfo
        {
            get
            {
                return _extendInfo;

            }
            set
            {
                _extendInfo = value;
                NotifyPropertyChanged("ExtendInfo");
            }
        }

        #endregion

    


        public void ShowDetial(PrivegeTypes privegeTypes,string DataKey)
        {
            DetailPrivege = privegeTypes;
            switch (privegeTypes)
            {
                case PrivegeTypes.New:
                    CW_ELMItemDetail cw = new CW_ELMItemDetail(this);
                    cw.Show();
                    break;
                default:
                    _domaincontext = new ELMServiceClient();
                    _domaincontext.GetELMItemByIdAsync(new Guid(DataKey));
                    _domaincontext.GetELMItemByIdCompleted += (sender, data) =>
                    {
                        if (data.Error == null)
                        {
                            CurrentElmItem = data.Result;                           
                            cw = new CW_ELMItemDetail(this);
                            cw.Show();
                        }
                        else
                        {
                            throw data.Error;
                        }
                    };
                    break;
            }
           

            
        }

        

        #region IAddUserPrentationModel 成员

        private PrivegeTypes _detailPrivege;
        public PrivegeTypes DetailPrivege
        {
            get
            {
                return _detailPrivege;
            }
            set
            {
                _detailPrivege = value;
                NotifyPropertyChanged("DetailPrivege");
            }
        }

        #endregion

        #region IAddUserPrentationModel 成员


      
        public void SetAddingHuman(string Hmid)
        {
            
             
           
        }

        #endregion

     

        #region IAddUserPrentationModel 成员


        public void delUser(Human hm)
        {
            BusyMessage.Show();
            _domaincontext = new ELMServiceClient();
            _domaincontext.DelHumanAsync(hm);
            _domaincontext.DelHumanCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        BusyMessage.Close();
                        initialData();
                    }
                };
        }

        #endregion

        #region IAddUserPrentationModel Members


        public void delItem(string hmid)
        {
            BusyMessage.Show();
            _domaincontext = new ELMServiceClient();
            _domaincontext.DelELMItemAsync(new Guid(hmid));
            _domaincontext.DelELMItemCompleted += (sender, data) =>
            {
                if (data.Error == null)
                {
                    BusyMessage.Close();
                    initialData();
                }
            };
        }

        #endregion

        #region IAddUserPrentationModel Members


        public ELMListUC ELMListData
        {
            get;
            set;
        }

        #endregion

        #region IELMItemsPrentationModel Members

        private ObservableCollection<ELMList> _allELMList;
        public ObservableCollection<ELMList> AllELMList
        {
            get
            {
                return _allELMList;
            }
            set
            {
                _allELMList = value;
                NotifyPropertyChanged("AllELMList");
            }
        }
        private ELMList _currentElmList;
        public ELMList CurrentElmList
        {
            get
            {
                return _currentElmList;
            }
            set
            {
                _currentElmList = value;
                NotifyPropertyChanged("CurrentElmList");
            }
        }

        #endregion

        #region IELMItemsPrentationModel Members

        private ELMItem _currentElmItem;
        public ELMItem CurrentElmItem
        {
            get
            {
                return _currentElmItem;
            }
            set
            {
                _currentElmItem = value;
                NotifyPropertyChanged("CurrentElmItem");
            }
        }

        public void BindELMList()
        {
            ELMListData.ClearAllData();
            _domaincontext = new ELMServiceClient();
            _domaincontext.GetAllELMListsAsync();
            _domaincontext.GetAllELMListsCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        AllELMList = data.Result;
                    }
                    else
                        throw data.Error;
                };
        }

        #endregion

        #region IELMItemsPrentationModel Members


        public void SaveData()
        {
            _domaincontext.UpdateELMItemAsync(CurrentElmItem);
            _domaincontext.UpdateELMItemCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        initialData();
                    }
                    else
                        throw data.Error;
                };
        }

        #endregion
    }
}
